<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="productOrders" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="orderSn" label="订单编码" width="120" sortable>
			</el-table-column>
			<el-table-column prop="price" label="支付总额" width="120" sortable>
			</el-table-column>
			<el-table-column prop="digest" label="摘要" width="220" sortable>
			</el-table-column>
			<el-table-column prop="lastPayTime" label="最后支付时间" width="160" sortable>
			</el-table-column>
			<el-table-column prop="lastConfirmTime" label="最后确认时间" width="160" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" width="100" sortable>
				<template scope="scope">
					<span style="color: #5daf34" v-if="scope.row.state === 0">待支付</span>
					<span style="color:red" v-else-if="scope.row.state === 1">待发货</span>
					<span style="color:yellow" v-else-if="scope.row.state === 2">待确认</span>
					<span style="color:green" v-else-if="scope.row.state === 3">已完成</span>
					<span style="color:gray" v-else-if="scope.row.state === -1">取消</span>
					<span style="color:dimgrey" v-else>未知</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" >
				<template scope="scope">
					<el-button size="small" @click="orderInfo(scope.$index, scope.row)">详情</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">取消</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :page-size="pageSize"
						   :current-page="currentPage"
						   :total="total"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<el-dialog title="订单详情" :visible.sync="productOrderInfoVisible" :close-on-click-modal="false">
			<el-form :model="productOrderInfo" label-width="120px" ref="productOrderInfo">
				<el-form-item label="摘要" prop="digest">
					<el-input readonly="readonly" v-model="productOrderInfo.digest" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="订单号" prop="orderSn">
					<el-input readonly="readonly" v-model="productOrderInfo.orderSn" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="价格" prop="price">
					<el-input readonly="readonly" v-model="productOrderInfo.price" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="状态"   prop="state">
                    <el-radio-group v-model="productOrderInfo.state">
                        <el-radio class="radio" :label="0">待支付</el-radio>
                        <el-radio class="radio" :label="1">待发货</el-radio>
                        <el-radio class="radio" :label="2">待确认</el-radio>
                        <el-radio class="radio" :label="3">已完成</el-radio>
                        <el-radio class="radio" :label="-1">取消</el-radio>
                    </el-radio-group>
					<!--<template scope="scope">
                        <span style="color: #5daf34" v-if="scope.row.state === 0">待支付</span>
                        <span style="color:red" v-else-if="scope.row.state === 1">待发货</span>
                        <span style="color:yellow" v-else-if="scope.row.state === 2">待确认</span>
                        <span style="color:green" v-else-if="scope.row.state === 3">已完成</span>
                        <span style="color:gray" v-else-if="scope.row.state === -1">取消</span>
                        <span style="color:dimgrey" v-else>未知</span>
					</template>-->
				</el-form-item>
				<el-form-item label="资源" v-model="productOrderInfo.detail.resources" prop="resources">
					<!--<el-input v-model="editForm.resources" auto-complete="off"></el-input>-->
					<el-upload
							class="upload-demo"
							action="http://localhost:8080/fastDfs"
							:file-list="fileList"
							list-type="picture">
						<!--<el-button size="small" type="primary">点击上传</el-button>
						<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
					</el-upload>
				</el-form-item>
				<el-form-item label="最后确认时间" prop="lastConfirmTime">
					<el-input v-model="productOrderInfo.lastConfirmTime" auto-complete="off"></el-input>
				</el-form-item>

				<el-form-item label="服务名称" prop="manager">
					<el-input readonly="readonly" v-model="productOrderInfo.detail.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="下单人" prop="parent">
					<el-input readonly="readonly" v-model="productOrderInfo.user.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="门店" prop="parent">
					<el-input readonly="readonly" v-model="productOrderInfo.shop.name" auto-complete="off"></el-input>
				</el-form-item>
                <el-form-item label="服务次数" prop="salecount">
                    <el-input readonly="readonly" v-model="productOrderInfo.detail.salecount" auto-complete="off"></el-input>
                </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="productOrderInfoVisible = false">取消</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
    export default {
        data() {
            return {
                title:'',
                filters: {
                    keyword: ''
                },
                productOrders: [],
                fileList:[],
                total: 0,
                pageSize:10,
                currentPage: 1,
                listLoading: false,
                sels: [],//列表选中行
                productOrderInfoVisible:false,
                productOrderInfo: {
                    digest: '',
                    state: 0,
                    price: 0,
                    orderSn: null,
					name:'',
                    paySn: null,

                    lastPayTime:'',
                    lastConfirmTime: '',
                    detail: {
                        resources:'',
                        salecount:0
                    },
                    user: {},
                    shop: {}
                }
            }
        },
        methods: {
            orderInfo(index,row){
                let orderId = row.id;
                this.$http.get("/productOrder/"+orderId)
                    .then(result=>{
                        this.productOrderInfo = result.data;
                        console.log(this.productOrderInfo);
                        this.productOrderInfoVisible = true;//显示编辑窗口
                    })
                    .catch(result=>{

                    })
                //图片回显
                this.fileList = [];
                var resources =  this.productOrderInfo.detail.resources;
                var resourcesArr = resources.split(",");
                for(var i=0;i<resourcesArr.length;i++){
                    this.fileList.push({url:"http://47.95.117.210:8888"+resourcesArr[i],
                        response:{resultObj:resourcesArr[i]}
                    })
                }
                console.log(fileList);
            },
            search(){//高级查询需要定位到第一页
                this.currentPage = 1;
                this.getProductOrders();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getProductOrders();
            },
            //获取服务订单列表
            getProductOrders() {
                let para = {
                    currentPage: this.currentPage,
                    keyword: this.filters.keyword,
                    pageSize:this.pageSize
                };
                this.listLoading = true;//开启忙等框
                this.$http.post("/productOrder/admin",para)
                    .then(result=>{
                        result = result.data;//PageList{total list}
                        this.productOrders = result.list;
                        this.listLoading = false;//关闭忙等框
                        this.total = result.total;

                    })
                    .catch(result=>{
                        console.log(result);
                        alert("系统错误！");
                    })
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认取消该订单吗?', '温馨提示', {
                    type: 'warning'
                }).then(() => {//点击确定
                    this.listLoading = true;//开启忙等框
                    //NProgress.start();
                    this.$http.put("/productOrder/"+row.id)
                        .then(result=>{
                            //1.关闭忙等
                            this.listLoading = false;
                            //判断业务操作是否正常
                            result  = result.data;//AjaxResult  {success:false,message:操作失败}
                            if(result.success){
                                this.$message({
                                    message: '取消成功',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.message,//提示我们自己的业务错
                                    type: 'error'
                                });
                            }
                            //2.刷新页面
                            //是否需要重新刷新页面   要  定位到第一页
                            this.currentPage = 1;
                            this.getProductOrders();
                        })
                        .catch(result=>{
                            this.listLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                            //失败了是否需要重新刷新页面   要  定位到第一页
                            this.currentPage = 1;
                            this.getProductOrders();
                        })
                }).catch(() => {//点击取消

                });
            },
            selsChange: function (sels) {//当前已经选中的所有行的数据
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id);//[1,2,3]
                this.$confirm('确认删除选中记录吗？', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    this.$http.patch("/dept",ids)
                        .then(result=>{
                            //1.关闭忙等
                            this.listLoading = false;
                            //判断业务操作是否正常
                            result  = result.data;//AjaxResult  {success:false,message:操作失败}
                            if(result.success){
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.message,//提示我们自己的业务错
                                    type: 'error'
                                });
                            }
                            //2.刷新页面
                            //是否需要重新刷新页面   要  定位到第一页
                            this.currentPage = 1;
                            this.getProductOrders();
                        })
                        .catch(result=>{
                            this.listLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                            //失败了是否需要重新刷新页面   要  定位到第一页
                            this.currentPage = 1;
                            this.getProductOrders();
                        })
                }).catch(() => {

                });
            }
        },
        mounted() {
            this.getProductOrders();
        }
    }

</script>

<style scoped>


</style>